//
//  Card.m
//  CardSwitchDemo
//
//  Created by Apple on 2016/11/9.
//  Copyright © 2016年 Apple. All rights reserved.
//

#import "XLTargetCard.h"
#import "XLCardItem.h"
#import "SINAUiviewClickLiserner.h"
#import "SINACalendarUtils.h"
#import "SINACircularBeadUtils.h"
#import "HWCircleView.h"

@interface XLTargetCard () {
    
    
    
    //日期
    UILabel *_dayLabel;
    //时间
    UILabel *_timeLabel;
    UILabel *_timeDiviLabel;
    //消息
    UILabel *_msgContentLabel;
    UIView  *_mainView;
    UIView *_bgView;
    
    UIView *_noDataView;
    UIView *_haveDataView;
    UIView *_finishDataView;
    
    UILabel *_noDataTitleLabel;
    UILabel *_noDataTitleFlagLabel;
    UIImageView *_noDataImageViewIcon;
    
    UILabel *_haveDataTitleLabel;
    UIImageView *_haveDataImageViewIcon;
    UIImageView *_haveTargetStatueViewIcon;
    UILabel *_flagTimeTitleLabel;
    UILabel *_currentTimeTitleLabel;
    
    UILabel *_finishLabel;
    
    HWCircleView *_circleView;
    UIImageView *_circleImageViewIcon;
    NSInteger _useTotalTime;
    
    
    UIView *_flagTitleFooterView;
    UIView *_flagTimeFooterView;
    UIView *_flagStatueFooterView ;
    
}



@property(strong,nonatomic)UIImageView *titleImageView;

@property(strong,nonatomic) UILabel *savePhotoUILabel;
@property(strong,nonatomic) UILabel *userNikeNameUILabel;
@property(strong,nonatomic) UILabel *userFlagUILabel;
@property(strong,nonatomic) UIView *deleteView;
@property(strong,nonatomic) UILabel *targetEndTimeUILabel;
@property(strong,nonatomic) UILabel *targetEndTimeUILabel2;
@property(strong,nonatomic) UILabel *targetTitleUILabel;
@property(strong,nonatomic) UILabel *targetTitlleFlagUILabel;
@property(strong,nonatomic) UILabel *targetStatueLabel;

@property(strong,nonatomic) UILabel *targetUseTimeLabel;
@property(strong,nonatomic) UILabel *targetUseTotalLabel;

@property(strong,nonatomic)UIImageView *finishImageView;

@end

@implementation XLTargetCard

- (instancetype)initWithFrame:(CGRect)frame {
    
    return [self initWithFrame:frame :YES];
}
- (instancetype)initWithFrame:(CGRect)frame :(BOOL)isShowTitle{
    if (self = [super initWithFrame:frame]) {
        [self buildUI:isShowTitle];
    }
    return self;
}
- (void)buildUI :(BOOL)isShowTitle{
    self.layer.cornerRadius =7.0f;
    self.layer.masksToBounds = true;
    self.backgroundColor = [UIColor whiteColor];
    
//    CGFloat labelHeight = self.bounds.size.height;
    
    CGFloat labelWidth = self.bounds.size.width;
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(valueChanged:) name:@"closeTimer" object:nil];
    
    
    _bgView=[[UIView alloc]initWithFrame:CGRectMake(20, 20, self.bounds.size.width-40, self.bounds.size.height-40)];
    _bgView.backgroundColor=[UIColor blueColor];
    [self addSubview:_bgView];
    
    _mainView=[[UIView alloc]initWithFrame:CGRectMake(20, 20, self.bounds.size.width-40, self.bounds.size.height-40)];
    _mainView.backgroundColor=[UIColor whiteColor];
    [self addSubview:_mainView];
    
    _finishDataView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
    _finishDataView.backgroundColor=[UIColor whiteColor];
    [self addSubview:_finishDataView];
    
    
    
    
    _dayLabel=[[UILabel alloc]initWithFrame:CGRectMake(15,4,labelWidth-15*2,21)];
    _dayLabel.font = [UIFont systemFontOfSize:14];
    _dayLabel.textAlignment = NSTextAlignmentLeft;
    _dayLabel.font = [UIFont fontWithName:@ "Arial Rounded MT Bold"  size:(13)];
    _dayLabel.textColor = [UIColor blackColor];
    _dayLabel.text=@"****-**-**";
    
    
    _timeLabel=[[UILabel alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(_dayLabel.frame)-4,labelWidth-15*2,21)];
    _timeLabel.font = [UIFont systemFontOfSize:14];
    _timeLabel.textAlignment = NSTextAlignmentLeft;
    _timeLabel.textColor = [UIColor colorWithHexString:@"#2e2e2e"];
    _timeLabel.text=@"--:--";
    
    
    _timeDiviLabel=[[UILabel alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(_timeLabel.frame),38,2)];
    _timeDiviLabel.backgroundColor=[UIColor colorWithHexString:@"#349ef7"];
    
    
    _msgContentLabel=[[UILabel alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(_timeLabel.frame)+4,labelWidth-15*2,42)];
    _msgContentLabel.font = [UIFont systemFontOfSize:13];
    _msgContentLabel.textAlignment = NSTextAlignmentLeft;
    _msgContentLabel.numberOfLines=2;
    _msgContentLabel.textColor = [UIColor colorWithHexString:@"#2e2e2e"];
    _msgContentLabel.text=@"--";
    
    
    
    _noDataView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,  CGRectGetWidth(_mainView.frame),CGRectGetHeight(_mainView.frame))];
    _haveDataView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,  CGRectGetWidth(_mainView.frame),CGRectGetHeight(_mainView.frame))];
    
    [_mainView addSubview:_noDataView];
    [_mainView addSubview:_haveDataView];
    
    
    [self initNoDataView];
    [self initHaveDataView];
    [self initFinishTargetViewFunction];
    
    //活动未组队 左滑删除
    
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(itemView4HandleSwipeFrom:)];
    //设置属性，swipe也是有两种属性设置手指个数及轻扫方向
    swipe.numberOfTouchesRequired = 1;
    //设置轻扫方向(默认是从左往右)
    //direction是一个枚举值有四个选项，我们可以设置从左往右，从右往左，从下往上以及从上往下
    //设置轻扫方向(默认是从左往右)
    [swipe setDirection:(UISwipeGestureRecognizerDirectionUp)];
    [_haveDataView addGestureRecognizer:swipe];
    UISwipeGestureRecognizer *swipe2 = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(itemView4HandleSwipeFrom:)];
    //设置属性，swipe也是有两种属性设置手指个数及轻扫方向
    swipe2.numberOfTouchesRequired = 1;
    //设置轻扫方向(默认是从左往右)
    //direction是一个枚举值有四个选项，我们可以设置从左往右，从右往左，从下往上以及从上往下
    //设置轻扫方向(默认是从左往右)
    [swipe2 setDirection:(UISwipeGestureRecognizerDirectionDown)];
    [_haveDataView addGestureRecognizer:swipe2];
    _haveDataView.userInteractionEnabled = YES;
    
    
    
}
-(void)initNoDataView{
    _noDataTitleLabel=[[UILabel alloc]initWithFrame:CGRectMake(0,8,CGRectGetWidth(_noDataView.frame),21)];
    _noDataTitleLabel.font = [UIFont systemFontOfSize:14];
    _noDataTitleLabel.textAlignment = NSTextAlignmentLeft;
    _noDataTitleLabel.font = [UIFont fontWithName:@ "Arial Rounded MT Bold"  size:(18)];
    _noDataTitleLabel.textColor = [UIColor blackColor];
    _noDataTitleLabel.textAlignment=NSTextAlignmentCenter;
    _noDataTitleLabel.text=@"添加小目标";
    [_noDataView addSubview:_noDataTitleLabel];
    
    
    _noDataTitleFlagLabel=[[UILabel alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(_noDataTitleLabel.frame)+10,CGRectGetWidth(_mainView.frame)-20*2,42)];
    _noDataTitleFlagLabel.font = [UIFont systemFontOfSize:14];
    _noDataTitleFlagLabel.numberOfLines=2;
    _noDataTitleFlagLabel.textAlignment = NSTextAlignmentCenter;
    _noDataTitleFlagLabel.textColor = [UIColor colorWithHexString:@"#878787"];
    _noDataTitleFlagLabel.text=@"小目标小目标小目标小目标小目标小目标小目标小目标标";
    [_noDataView addSubview:_noDataTitleFlagLabel];
    
    //no_target_icon
    
    _noDataImageViewIcon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"no_target_icon"]];
    _noDataImageViewIcon.frame=CGRectMake(CGRectGetWidth(_noDataView.frame)/2-211/2, CGRectGetMaxY(_noDataTitleFlagLabel.frame)+40, 211, 216);
    [_noDataView addSubview:_noDataImageViewIcon];
    
    
    
    
}
-(void)initHaveDataView{
    _haveDataTitleLabel=[[UILabel alloc]initWithFrame:CGRectMake(0,8,CGRectGetWidth(_noDataView.frame),21)];
    _haveDataTitleLabel.font = [UIFont systemFontOfSize:14];
    _haveDataTitleLabel.textAlignment = NSTextAlignmentLeft;
    _haveDataTitleLabel.font = [UIFont fontWithName:@ "Arial Rounded MT Bold"  size:(18)];
    _haveDataTitleLabel.textColor = [UIColor blackColor];
    _haveDataTitleLabel.textAlignment=NSTextAlignmentCenter;
    _haveDataTitleLabel.text=@"小目标";
    [_haveDataView addSubview:_haveDataTitleLabel];
    
    
    
    //no_target_icon
    
    _haveDataImageViewIcon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"target_cir_bg"]];
    _haveDataImageViewIcon.frame=CGRectMake(CGRectGetWidth(_noDataView.frame)/2-150/2, CGRectGetMaxY(_haveDataTitleLabel.frame)+40, 150, 150);
    [_haveDataView addSubview:_haveDataImageViewIcon];
    
    //定时显示方式
    _circleView = [[HWCircleView alloc] init];
    _circleView.frame =_haveDataImageViewIcon.frame;
    _circleView.progressStoreWidth=15;
    _circleView.progressColor = baseButtonBackGroundColor;
    _circleView.hidden=YES;
    [_haveDataView addSubview:_circleView];
    
    //不定时显示 方式
    _circleImageViewIcon= [[UIImageView alloc] init];
    _circleImageViewIcon.image=[UIImage  imageNamed:@"target_cir_leve_bg"];
    _circleImageViewIcon.frame =_haveDataImageViewIcon.frame;
    _circleImageViewIcon.hidden=YES;
    [_haveDataView addSubview:_circleImageViewIcon];
    
    
    _haveTargetStatueViewIcon= [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"target_cir_start_bg"]];
    _haveTargetStatueViewIcon.frame=CGRectMake(CGRectGetWidth(_noDataView.frame)/2-38/2, CGRectGetMaxY(_noDataTitleFlagLabel.frame)+40, 38, 41);
    [_haveDataView addSubview:_haveTargetStatueViewIcon];
    
    
    
    
    
    _flagTimeTitleLabel=[[UILabel alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(_haveDataImageViewIcon.frame)+20,CGRectGetWidth(_mainView.frame)-20*2,21)];
    _flagTimeTitleLabel.font = [UIFont systemFontOfSize:14];
    _flagTimeTitleLabel.numberOfLines=2;
    _flagTimeTitleLabel.textAlignment = NSTextAlignmentCenter;
    _flagTimeTitleLabel.textColor = [UIColor colorWithHexString:@"#878787"];
    _flagTimeTitleLabel.text=@"目标:00min";
    [_haveDataView addSubview:_flagTimeTitleLabel];
    
    _currentTimeTitleLabel=[[UILabel alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(_haveDataImageViewIcon.frame)+50,CGRectGetWidth(_mainView.frame)-20*2,21)];
    _currentTimeTitleLabel.font = [UIFont systemFontOfSize:22];
    _currentTimeTitleLabel.numberOfLines=2;
    _currentTimeTitleLabel.textAlignment = NSTextAlignmentCenter;
    _currentTimeTitleLabel.textColor = baseSelectColor;
    _currentTimeTitleLabel.text=@"00:00:00";
    [_haveDataView addSubview:_currentTimeTitleLabel];
    
    _finishLabel=[[UILabel alloc]initWithFrame:CGRectMake(30, CGRectGetMaxY(_currentTimeTitleLabel.frame)+40,CGRectGetWidth(_mainView.frame)-30*2,36)];
    _finishLabel.font = [UIFont systemFontOfSize:18];
    _finishLabel.numberOfLines=2;
    _finishLabel.textAlignment = NSTextAlignmentCenter;
    _finishLabel.textColor = [UIColor whiteColor];
    _finishLabel.text=@"完成";
    _finishLabel.backgroundColor=[UIColor colorWithHexString:@"#d0d0d0"];
    [_haveDataView addSubview:_finishLabel];
    [SINACircularBeadUtils makeCircularView:_finishLabel :5];
    
}

-(void) initFinishTargetViewFunction{
    UIImageView *bgImageView = [[UIImageView alloc]init];
    bgImageView.frame =CGRectMake(0, 0,CGRectGetWidth(_finishDataView.frame), CGRectGetHeight(_finishDataView.frame));
    //[_finishDataView addSubview:bgImageView];
    bgImageView.image =[UIImage imageNamed:@"target_finish_bg"];
    
    
    
    UIImageView *titleImageView = [[UIImageView alloc]init];
    titleImageView.frame =CGRectMake(26, 36,64,64);
    [_finishDataView addSubview:titleImageView];
    titleImageView.image =[UIImage imageNamed:@"默认头像"];
    [SINACircularBeadUtils makeCircularView:titleImageView];
    self.titleImageView=titleImageView;
    
    
    
    UILabel *userNikeNameUILabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.titleImageView.frame)+16,36+10, CGRectGetWidth(_finishDataView.frame)-CGRectGetMaxX(self.titleImageView.frame)-10-16, 21)];
    userNikeNameUILabel.text=@"-- --";
    userNikeNameUILabel.font=[UIFont systemFontOfSize:16];
    userNikeNameUILabel.textColor=[UIColor colorWithHexString:@"#212121"];
    self.userNikeNameUILabel=userNikeNameUILabel;
    [_finishDataView addSubview:userNikeNameUILabel];
    
    
    UILabel *userFlagUILabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.titleImageView.frame)+16,CGRectGetMaxY(userNikeNameUILabel.frame)+4, CGRectGetWidth(_finishDataView.frame)-CGRectGetMaxX(self.titleImageView.frame)-10-16, 21)];
    userFlagUILabel.text=@"-- --";
    userFlagUILabel.font=[UIFont systemFontOfSize:12];
    userFlagUILabel.textColor=[UIColor colorWithHexString:@"#2e2e2e"];
    self.userFlagUILabel=userFlagUILabel;
    [_finishDataView addSubview:userFlagUILabel];
    
    
    
    
    UIImageView *finishImageView = [[UIImageView alloc]init];
    finishImageView.frame =CGRectMake(CGRectGetWidth(_finishDataView.frame)/2-74/2, CGRectGetMaxY(titleImageView.frame)+40,74,77);
    [_finishDataView addSubview:finishImageView];
    finishImageView.image =[UIImage imageNamed:@"target_no_finish_icon"];
    self.finishImageView =finishImageView;
    
  
    
    
    
    UILabel *targetEndTimeUILabel = [[UILabel alloc]initWithFrame:CGRectMake(15,CGRectGetMaxY(titleImageView.frame)+26, CGRectGetWidth(_finishDataView.frame)-30, 21)];
    targetEndTimeUILabel.text=@"星期 -- --月--日";
    targetEndTimeUILabel.font=[UIFont systemFontOfSize:12];
    targetEndTimeUILabel.textColor=[UIColor colorWithHexString:@"#878787"];
    self.targetEndTimeUILabel=targetEndTimeUILabel;
    //[_finishDataView addSubview:targetEndTimeUILabel];
    
   
    
    
   
    
    UILabel *targetTitleUILabel = [[UILabel alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(finishImageView.frame)+30, CGRectGetWidth(_finishDataView.frame), 21)];
    targetTitleUILabel.text=@"跑步:已达标";
    targetTitleUILabel.textAlignment=NSTextAlignmentCenter;
    targetTitleUILabel.font=[UIFont systemFontOfSize:16];
    targetTitleUILabel.textColor=baseSelectColor;
    self.targetTitleUILabel=targetTitleUILabel;
    [_finishDataView addSubview:targetTitleUILabel];
    
    
    UILabel *targetEndTimeUILabel2 = [[UILabel alloc]initWithFrame:CGRectMake(15,CGRectGetMaxY(targetTitleUILabel.frame)+4, CGRectGetWidth(_finishDataView.frame)-30, 21)];
    targetEndTimeUILabel2.text=@"--:--:--";
    targetEndTimeUILabel2.font=[UIFont systemFontOfSize:14];
    targetEndTimeUILabel2.textAlignment=NSTextAlignmentCenter;
    targetEndTimeUILabel2.textColor=[UIColor colorWithHexString:@"#878787"];
    self.targetEndTimeUILabel2=targetEndTimeUILabel2;
    [_finishDataView addSubview:targetEndTimeUILabel2];
   
    
    
    UILabel *selectUILabel = [[UILabel alloc]initWithFrame:CGRectMake(15,CGRectGetHeight(_finishDataView.frame)-15-32, CGRectGetWidth(_finishDataView.frame)-30, 32)];
    selectUILabel.text=@"确定";
    selectUILabel.font=[UIFont systemFontOfSize:14];
    selectUILabel.textAlignment=NSTextAlignmentCenter;
    selectUILabel.backgroundColor = baseSelectColor;
    selectUILabel.textColor=[UIColor colorWithHexString:@"#ffffff"];
    [SINACircularBeadUtils makeCircularBead:16 andView:selectUILabel ];
    
    
   
    
    //目标
    UIView *flagTitleFooterView = [[UIView alloc]initWithFrame:CGRectMake(15, CGRectGetMinY(selectUILabel.frame)-66, (CGRectGetWidth(_finishDataView.frame)-30)/3, 60)];
    flagTitleFooterView.backgroundColor=[UIColor clearColor];
    _flagTitleFooterView=flagTitleFooterView;
    [_finishDataView addSubview:flagTitleFooterView];
    
    UILabel *titleFlag1Label = [[UILabel alloc]initWithFrame:CGRectMake(15,0, CGRectGetWidth(flagTitleFooterView.frame)-15, 21)];
    titleFlag1Label.text=@"目标";
    titleFlag1Label.font=[UIFont systemFontOfSize:12];
    titleFlag1Label.textColor=[UIColor colorWithHexString:@"#000000"];
    [flagTitleFooterView addSubview:titleFlag1Label];
    
    UIView *diviFlag1View = [[UIView alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(titleFlag1Label.frame)+4, 15, 2)];
    diviFlag1View.backgroundColor=[UIColor blackColor];
    
    [flagTitleFooterView addSubview:diviFlag1View];
    
    UILabel *titleFlagValue1Label = [[UILabel alloc]initWithFrame:CGRectMake(15,CGRectGetMinY(diviFlag1View.frame)+6, CGRectGetWidth(flagTitleFooterView.frame)-15, 21)];
    titleFlagValue1Label.text=@"30min";
    titleFlagValue1Label.font=[UIFont systemFontOfSize:12];
    titleFlagValue1Label.textColor=[UIColor colorWithHexString:@"#000000"];
    [flagTitleFooterView addSubview:titleFlagValue1Label];
    self.targetUseTotalLabel =titleFlagValue1Label;
    
    
    //时长
    UIView *flagTimeFooterView = [[UIView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(flagTitleFooterView.frame), CGRectGetMinY(selectUILabel.frame)-66, (CGRectGetWidth(_finishDataView.frame)-30)/3, 60)];
    flagTimeFooterView.backgroundColor=[UIColor clearColor];
    _flagTimeFooterView=flagTimeFooterView;
    [_finishDataView addSubview:flagTimeFooterView];
    
    UILabel *titleFlag2Label = [[UILabel alloc]initWithFrame:CGRectMake(15,0, CGRectGetWidth(flagTitleFooterView.frame)-15, 21)];
    titleFlag2Label.text=@"时长";
    titleFlag2Label.font=[UIFont systemFontOfSize:12];
    titleFlag2Label.textColor=[UIColor colorWithHexString:@"#000000"];
    [flagTimeFooterView addSubview:titleFlag2Label];
    
    UIView *diviFlag2View = [[UIView alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(titleFlag2Label.frame)+4, 15, 2)];
    diviFlag2View.backgroundColor=[UIColor blackColor];
    
    [flagTimeFooterView addSubview:diviFlag2View];
    
    UILabel *titleFlagValue2Label = [[UILabel alloc]initWithFrame:CGRectMake(15,CGRectGetMinY(diviFlag2View.frame)+6, CGRectGetWidth(flagTitleFooterView.frame)-15, 21)];
    titleFlagValue2Label.text=@"00'00'00";
    titleFlagValue2Label.font=[UIFont systemFontOfSize:12];
    titleFlagValue2Label.textColor=[UIColor colorWithHexString:@"#000000"];
    [flagTimeFooterView addSubview:titleFlagValue2Label];
    self.targetUseTimeLabel =titleFlagValue2Label;
    
    //状态
    UIView *flagStatueFooterView = [[UIView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(flagTimeFooterView.frame), CGRectGetMinY(selectUILabel.frame)-66, (CGRectGetWidth(_finishDataView.frame)-30)/3, 60)];
    flagStatueFooterView.backgroundColor=[UIColor clearColor];
    _flagStatueFooterView =flagStatueFooterView ;
    [_finishDataView addSubview:flagStatueFooterView];
    
    UILabel *titleFlag3Label = [[UILabel alloc]initWithFrame:CGRectMake(15,0, CGRectGetWidth(flagTitleFooterView.frame)-15, 21)];
    titleFlag3Label.text=@"状态";
    titleFlag3Label.font=[UIFont systemFontOfSize:12];
    titleFlag3Label.textColor=[UIColor colorWithHexString:@"#000000"];
    [flagStatueFooterView addSubview:titleFlag3Label];
    
    UIView *diviFlag3View = [[UIView alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(titleFlag3Label.frame)+4, 15, 2)];
    diviFlag3View.backgroundColor=[UIColor blackColor];
    
    [flagStatueFooterView addSubview:diviFlag3View];
    
    UILabel *titleFlagValue3Label = [[UILabel alloc]initWithFrame:CGRectMake(15,CGRectGetMinY(diviFlag3View.frame)+6, CGRectGetWidth(flagTitleFooterView.frame)-15, 21)];
    titleFlagValue3Label.text=@"已达标";
    titleFlagValue3Label.font=[UIFont systemFontOfSize:12];
    titleFlagValue3Label.textColor=[UIColor colorWithHexString:@"#000000"];
    [flagStatueFooterView addSubview:titleFlagValue3Label];
    self.targetStatueLabel = titleFlagValue3Label;
}
- (void)itemView4HandleSwipeFrom:(UISwipeGestureRecognizer *)recognizer{
    
    if (recognizer.view==_haveDataView) {
        if (self.item.actTargetModel!=nil) {
            if (self.swipeBlock) {
                if(recognizer.direction == UISwipeGestureRecognizerDirectionUp) {
                    self.swipeBlock(self, 1);
                }else if(recognizer.direction == UISwipeGestureRecognizerDirectionDown) {
                    self.swipeBlock(self, 2);
                }
            }
        }
    }
}

-(void)setItem:(XLCardItem *)item {
    _item = item;
    SINAStuActivityTargetModel *actTargetModel = item.actTargetModel;
    actTargetModel.useTimes=actTargetModel.useTime;
    if (actTargetModel==nil) {
        _noDataView.hidden=NO;
        _noDataView.alpha=0.0;
        _finishDataView.hidden=YES;
        
        [UIView animateWithDuration:0.2 animations:^{
            _noDataView.alpha=1.0;
            _haveDataView.alpha=0.0;
        } completion:^(BOOL finished) {
            _noDataView.alpha=1.0;
            _haveDataView.hidden=YES;
        }];
        
        [[SINAUiviewClickLiserner alloc]viewClickFunction:_noDataView andWithBlick:^(UIView *view) {
            if (self.clickBlock) {
                self.clickBlock(_noDataView,0);
            }
        }];
        
    } else {
        
        if (self.item.actTargetModel.status==1014) {
            _finishDataView.hidden=NO;
            _haveDataView.hidden=YES;
            _finishDataView.alpha=0.0;
          
            
            [UIView animateWithDuration:0.2 animations:^{
                _haveDataView.alpha=0.0;
                _finishDataView.alpha=1.0;
            } completion:^(BOOL finished) {
                _noDataView.hidden=YES;
                _haveDataView.hidden=YES;
                _finishDataView.alpha=1.0;
            }];
            
            SINAUserModel *userModel = [[SINACommonUserHelper share]getUserModel];
            NSString *headerImageUrl = userModel.userTitleImageUrl;
            if (headerImageUrl!=nil&&headerImageUrl.length>1) {
                NSURL *imageUrl =[NSURL URLWithString:headerImageUrl];
                [self.titleImageView sd_setImageWithURL:imageUrl placeholderImage:[UIImage imageNamed:@"默认头像"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
                    DLog(@"加载头像完成");
                }];
            }
            
            
            NSString *userNikName = userModel.userNike;
            if (userNikName==nil||[userNikName isEqualToString:@""]) {
                NSString *phone = userModel.userPhone;
                if (phone==nil||[phone isEqualToString:@""]) {
                    phone=@"设置昵称";
                }else{
                    phone=@"设置昵称";
                }
                userNikName = phone;
            }
            self.userNikeNameUILabel.text=userNikName;
            
            NSString *sign =userModel.sign;
            if (sign==nil||sign.length==0||[sign isEqualToString:@"(null)"]) {
                sign=@"这个世界很美，不仿写个签名吧";
            }
            self.userFlagUILabel.text = sign;
            
            
            self.deleteView.hidden=NO;
            
            [self updateTimeFunction];
            
            
            
            if (self.item.actTargetModel.plantime!=nil&&![self.item.actTargetModel.plantime isEqualToString:@""]) {
                if ([self.item.actTargetModel.plantime isEqualToString:@"1min"]) {
                    _useTotalTime =1;
                }else  if ([self.item.actTargetModel.plantime isEqualToString:@"5min"]) {
                    _useTotalTime =5;
                }else  if ([self.item.actTargetModel.plantime isEqualToString:@"10min"]) {
                    _useTotalTime =10;
                }else  if ([self.item.actTargetModel.plantime isEqualToString:@"30min"]) {
                    _useTotalTime =30;
                }else  if ([self.item.actTargetModel.plantime isEqualToString:@"1hour"]) {
                    _useTotalTime =60;
                }else  if ([self.item.actTargetModel.plantime isEqualToString:@"2hour"]) {
                    _useTotalTime =120;
                }
            }else{
                _useTotalTime=0;
            }
            
            NSString *statue=@"已成功";
            
           
            NSInteger sta=1;
            if (self.item.actTargetModel.plantime!=nil&&![self.item.actTargetModel.plantime isEqualToString:@""]) {
                statue=@"已达标";
                sta=2;
                if (self.item.actTargetModel.useTime<_useTotalTime*60*1000) {
                    statue= @"未达标";
                    sta=3;
                }
            }
            switch (sta) {
                case 1:
                    statue=@"已成功";
                    self.finishImageView.image =[UIImage imageNamed:@"target_finish_icon"];
                    break;
                case 2:
                    statue=@"已达标";
                    self.finishImageView.image =[UIImage imageNamed:@"target_finish_icon"];
                    break;
                case 3:
                    statue= @"未达标";
                    self.finishImageView.image =[UIImage imageNamed:@"target_no_finish_icon"];
                    //target_no_finish_icon
                    break;
                default:
                    break;
            }
            
            self.targetTitleUILabel.text=[NSString stringWithFormat:@"%@:%@",self.item.actTargetModel.title,statue];
            self.targetStatueLabel.text=statue;
            self.targetUseTimeLabel.text=[[[SINACalendarUtils alloc]init]getMMSSFromSS:[NSString stringWithFormat:@"%ld",self.item.actTargetModel.useTime/1000]];
            
            
            if (self.item.actTargetModel.plantime!=nil&&![self.item.actTargetModel.plantime isEqualToString:@""]) {
                _flagTitleFooterView.hidden=NO;
                
                self.targetUseTotalLabel.text =self.item.actTargetModel.plantime;
                
                CGRect timeFrame =  _flagTimeFooterView.frame;
                timeFrame.origin.x = CGRectGetMaxX(_flagTitleFooterView.frame);
                _flagTimeFooterView.frame=timeFrame;
                
                CGRect statueFrame =  _flagStatueFooterView.frame;
                statueFrame.origin.x = CGRectGetMaxX(_flagTimeFooterView.frame);
                _flagStatueFooterView.frame=statueFrame;
            }else{
                
                self.targetUseTotalLabel.text =@"";
                _flagTitleFooterView.hidden=YES;
                _flagTimeFooterView.frame=_flagTitleFooterView.frame;
                
                CGRect statueFrame =  _flagStatueFooterView.frame;
                statueFrame.origin.x = CGRectGetMaxX(_flagTimeFooterView.frame);
                _flagStatueFooterView.frame=statueFrame;
            }
            
            
        } else {
            _finishDataView.hidden=YES;
            _haveDataView.hidden=NO;
            _haveDataView.alpha=0.0;
            _haveTargetStatueViewIcon.center = _haveDataImageViewIcon.center;
            
            [UIView animateWithDuration:0.2 animations:^{
                _haveDataView.alpha=1.0;
            } completion:^(BOOL finished) {
                _noDataView.hidden=YES;
                _haveDataView.alpha=1.0;
            }];
            
            _haveDataTitleLabel.text = actTargetModel.title;
            if (actTargetModel.plantime==nil||[actTargetModel.plantime isEqualToString:@""]) {
                _flagTimeTitleLabel.hidden=YES;
            }else{
                _flagTimeTitleLabel.hidden=NO;
                _flagTimeTitleLabel.text=[NSString stringWithFormat:@"目标：%@",actTargetModel.plantime];
            }
            [[SINAUiviewClickLiserner alloc]viewClickFunction:_noDataView andWithBlick:^(UIView *view) {
                
            }];
            
            if (self.item.actTargetModel.plantime!=nil&&![self.item.actTargetModel.plantime isEqualToString:@""]) {
                if ([self.item.actTargetModel.plantime isEqualToString:@"1min"]) {
                    _useTotalTime =1;
                }else  if ([self.item.actTargetModel.plantime isEqualToString:@"5min"]) {
                    _useTotalTime =5;
                }else  if ([self.item.actTargetModel.plantime isEqualToString:@"10min"]) {
                    _useTotalTime =10;
                }else  if ([self.item.actTargetModel.plantime isEqualToString:@"30min"]) {
                    _useTotalTime =30;
                }else  if ([self.item.actTargetModel.plantime isEqualToString:@"1hour"]) {
                    _useTotalTime =60;
                }else  if ([self.item.actTargetModel.plantime isEqualToString:@"2hour"]) {
                    _useTotalTime =120;
                }
            }else{
                _useTotalTime=0;
            }
            
            
            
            [self updateStatueIconFunction];
            
            [self initDefaultfunction];
            
            [self updateStatueClickFunction];
        }
        
       
        
    }
    
}


-(void)updateStatueIconFunction{
    if (self.item.actTargetModel.status==1000) {
        //未开始  显示开始按钮
        _currentTimeTitleLabel.text=@"00:00:00";
    }else if (self.item.actTargetModel.status==1012||self.item.actTargetModel.status==1011) {
        //已开始  显示暂按钮
        
        NSInteger activityStartTime = [self.activityMmodel.activityStartTime integerValue];
        NSInteger activityEndTime = [self.activityMmodel.activityStopTime integerValue];
        //当前时间
        NSInteger currentTime = [[SINACalendarUtils currentTimeStr] integerValue];
        
        if (currentTime<activityStartTime) {
            DLog(@"活动未开始");
        }else if(currentTime>=activityStartTime&&currentTime<=(activityEndTime)){
            DLog(@"活动中");
        }else if(currentTime>=activityEndTime&&currentTime<=(activityEndTime+30*60*1000)){
            //活动结束 以及结束后的半小时
        }else{
            //超时
            currentTime =activityEndTime+30*60*1000;;
        }
        
        if (self.item.actTargetModel.useTimes==0) {
            //未暂停过
            NSInteger flagTime =currentTime-self.item.actTargetModel.startTime;
            self.item.actTargetModel.useTimes =flagTime;
            _currentTimeTitleLabel. text=[[[SINACalendarUtils alloc]init]getMMSSFromSS:[NSString stringWithFormat:@"%ld",self.item.actTargetModel.useTimes/1000]];
        }else{
            //有过暂停时间
            self.item.actTargetModel.useTimes = self.item.actTargetModel.useTimes+currentTime-self.item.actTargetModel.pauseendtime;
            _currentTimeTitleLabel.text=[[[SINACalendarUtils alloc]init]getMMSSFromSS:[NSString stringWithFormat:@"%ld",self.item.actTargetModel.useTimes/1000]];
        }
        DLog(@"检查时间问题 title %@   %ld",self.item.actTargetModel.title,self.item.actTargetModel.useTimes);
        
    }else if (self.item.actTargetModel.status==1013 ) {
        //已暂住  显示继续按钮
        //一定有暂停时间
        _currentTimeTitleLabel.text=[[[SINACalendarUtils alloc]init]getMMSSFromSS:[NSString stringWithFormat:@"%ld",self.item.actTargetModel.useTimes/1000]];
    }else if (self.item.actTargetModel.status==1014) {
        //已暂住  显示继续按钮
        //一定有暂停时间
        _currentTimeTitleLabel.text=[[[SINACalendarUtils alloc]init]getMMSSFromSS:[NSString stringWithFormat:@"%ld",self.item.actTargetModel.useTimes/1000]];
    }
}

//更新按钮状态
-(void)updateStatueClickFunction{
    
    
    
    
    [[SINAUiviewClickLiserner alloc]viewClickFunction:_haveTargetStatueViewIcon andWithBlick:^(UIView *view) {
        if (self.clickBlock) {
            [self commfunction:1];
        }
    }];
    [[SINAUiviewClickLiserner alloc]viewClickFunction:_finishLabel andWithBlick:^(UIView *view) {
        if (self.clickBlock) {
//            NSInteger activityStartTime = [self.activityMmodel.activityStartTime integerValue];
            NSInteger activityEndTime = [self.activityMmodel.activityStopTime integerValue];
            //当前时间
            NSInteger currentTime = [[SINACalendarUtils currentTimeStr] integerValue];
            
            if (self.item.actTargetModel.status==1000) {
                 self.clickBlock(_haveTargetStatueViewIcon, 7);
            }else if (self.item.actTargetModel.status==1014) {
                 self.clickBlock(_haveTargetStatueViewIcon, 8);
            }else{
            if (currentTime<=(activityEndTime+30*60*1000)){
                self.clickBlock(_haveTargetStatueViewIcon, 5);
            }else{
                //超时
                self.clickBlock(_haveTargetStatueViewIcon, 6);
            }
            }
            
            
        }
    }];
    
    
}
-(void)initDefaultfunction{
    //未开始  显示开始按钮
//    NSInteger activityStartTime = [self.activityMmodel.activityStartTime integerValue];
    NSInteger activityEndTime = [self.activityMmodel.activityStopTime integerValue];
    //当前时间
    NSInteger currentTime = [[SINACalendarUtils currentTimeStr] integerValue];
    if (currentTime<=(activityEndTime+30*60*1000)) {
        DLog(@"活动未开始");
        DLog(@"活动中");
        DLog(@"活动结束 以及结束后的半小时");
        
        if (self.item.actTargetModel.plantime==nil||[self.item.actTargetModel.plantime isEqualToString:@""]) {
            //无定时
            _circleView.hidden=YES;
            _circleImageViewIcon.hidden=NO;
            
        }else{
            //有定时
            _circleView.hidden=NO;
            _circleImageViewIcon.hidden=YES;
        }
        
        if (self.item.actTargetModel.status==1000) {
            //未开始状态
            _haveTargetStatueViewIcon.image=[UIImage imageNamed:@"target_cir_start_bg"];
            _finishLabel.backgroundColor=[UIColor colorWithHexString:@"#d0d0d0"];
        }else if (self.item.actTargetModel.status==1012||self.item.actTargetModel.status==1011) {
            //已开始 状态
            _haveTargetStatueViewIcon.image=[UIImage imageNamed:@"target_cir_pasue_bg"];
            _finishLabel.backgroundColor=baseButtonBackGroundColor;
            [self addTimer];
            if (_circleImageViewIcon.hidden==NO) {
                //开启动画
                [self startAnimationWithFlag];
            }
            
        }else if (self.item.actTargetModel.status==1013 ) {
            //暂停  状态
            _haveTargetStatueViewIcon.image=[UIImage imageNamed:@"target_cir_goon_bg"];
            _finishLabel.backgroundColor=baseButtonBackGroundColor;
        }else if (self.item.actTargetModel.status==1014) {
            //完成 状态
        }
    }else{
        //超时
        currentTime =activityEndTime+30*60*1000;;
        _haveTargetStatueViewIcon.image=[UIImage imageNamed:@"target_cir_start_bg"];
        _finishLabel.backgroundColor=[UIColor colorWithHexString:@"#d0d0d0"];
    }
    
    
}
-(void)commfunction:(NSInteger)flag{
    //未开始  显示开始按钮
//    NSInteger activityStartTime = [self.activityMmodel.activityStartTime integerValue];
    NSInteger activityEndTime = [self.activityMmodel.activityStopTime integerValue];
    //当前时间
    NSInteger currentTime = [[SINACalendarUtils currentTimeStr] integerValue];
    if (currentTime<=(activityEndTime+30*60*1000)) {
        DLog(@"活动未开始");
        DLog(@"活动中");
        DLog(@"活动结束 以及结束后的半小时");
        
        if (self.item.actTargetModel.plantime==nil||[self.item.actTargetModel.plantime isEqualToString:@""]) {
            //无定时
            _circleView.hidden=YES;
            _circleImageViewIcon.hidden=NO;
            
        }else{
            //有定时
            _circleView.hidden=NO;
            _circleImageViewIcon.hidden=YES;
        }
        
        if (self.item.actTargetModel.status==1000) {
            //未开始到开始
            if (flag==1) {
                self.clickBlock(_haveTargetStatueViewIcon, 1);
            }
            _haveTargetStatueViewIcon.image=[UIImage imageNamed:@"target_cir_pasue_bg"];
            _finishLabel.backgroundColor=baseButtonBackGroundColor;
            self.item.actTargetModel.status=1011;
            [self addTimer];
            if (_circleImageViewIcon.hidden==NO) {
                //开启动画
                [self startAnimationWithFlag];
            }
        }else if (self.item.actTargetModel.status==1012||self.item.actTargetModel.status==1011) {
            //已开始  到暂停
            if (flag==1) {
                self.clickBlock(_haveTargetStatueViewIcon, 3);
            }
            _haveTargetStatueViewIcon.image=[UIImage imageNamed:@"target_cir_goon_bg"];
            _finishLabel.backgroundColor=baseButtonBackGroundColor;
            
            self.item.actTargetModel.status=1013;
            [self removeTimer];
            
            [self stopAnimation];
            
            
        }else if (self.item.actTargetModel.status==1013 ) {
            //暂停 到重新开始
            if (flag==1) {
                self.clickBlock(_haveTargetStatueViewIcon, 4);
            }
            _haveTargetStatueViewIcon.image=[UIImage imageNamed:@"target_cir_pasue_bg"];
            _finishLabel.backgroundColor=baseButtonBackGroundColor;
            
            self.item.actTargetModel.status=1012;
            [self addTimer];
            if (_circleImageViewIcon.hidden==NO) {
                //开启动画
                [self startAnimationWithFlag];
            }
            
        }else if (self.item.actTargetModel.status==1014) {
            //完成
        }
    }else{
        //超时
        currentTime =activityEndTime+30*60*1000;;
        if (flag==1) {
            self.clickBlock(_haveTargetStatueViewIcon, 2);
        }
        _haveTargetStatueViewIcon.image=[UIImage imageNamed:@"target_cir_start_bg"];
        _finishLabel.backgroundColor=[UIColor colorWithHexString:@"#d0d0d0"];
    }
    
    
}

- (void)addTimer
{
    if (_timer!=nil) {
        [_timer invalidate];
        _timer = nil;
    }
    
    _timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
    [self.timerSet addObject:_timer];
}
- (void)removeTimer
{
    if (_timer!=nil) {
        [_timer invalidate];
        _timer = nil;
    }
}

- (void)timerAction
{
    
    self.item.actTargetModel.useTimes+=1000;
    //DLog(@"已使用时间 %ld",self.self.item.actTargetModel.useTimes);
    _currentTimeTitleLabel .text=[[[SINACalendarUtils alloc]init]getMMSSFromSS:[NSString stringWithFormat:@"%ld",self.self.item.actTargetModel.useTimes/1000]];
    _circleView.progress =self.item.actTargetModel.useTimes *1.0f/(_useTotalTime*60*1000);
    
    
    //有定时  运行时间超过定时
    if (_circleView.progress>=1) {
        //target_cir_over_time
        if (_circleView.hidden==NO) {
            _circleView.hidden=YES;
            _circleImageViewIcon.hidden=NO;
            [self startAnimationWithFlag];
        }
    }
    
    
}



- (void)startAnimationWithFlag

{
    [_circleImageViewIcon.layer removeAllAnimations];
    CABasicAnimation* rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
    
    rotationAnimation.duration = 5;
    
    rotationAnimation.cumulative = YES;
    
    rotationAnimation.repeatCount = HUGE_VALF;
    
    [_circleImageViewIcon.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
    
    
    
}
- (void)stopAnimation

{
    
    [_circleImageViewIcon.layer removeAllAnimations];
    
}
- (void)removeFromSuperview
{
    [self removeTimer];
    [self stopAnimation];
}

- (void)valueChanged:(NSNotification *)notification{
    // object 就是传过来的参数
    DLog(@"停止动画");
    [self stopAnimation];
    
}
// 在对象销毁的时候移除通知
-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


-(void)updateTimeFunction{
    if (self.item.actTargetModel.endTime!=0) {
//        NSString *week = [SINACalendarUtils dateWeekWithDateString:[NSString stringWithFormat:@"%ld",self.item.actTargetModel.endTime]];
        NSString *dayTime = [SINACalendarUtils getDateStringWithTimeStr:[NSString stringWithFormat:@"%ld",self.item.actTargetModel.endTime] andFormat:@"MM月dd日"];
        NSString *hourTime = [SINACalendarUtils getDateStringWithTimeStr:[NSString stringWithFormat:@"%ld",self.item.actTargetModel.endTime] andFormat:@"yyyy年MM月dd日 HH:mm:ss"];
        //self.targetEndTimeUILabel.text =[NSString stringWithFormat:@"%@ %@",week,dayTime];
        
        self.targetEndTimeUILabel2.text=hourTime;
    }
}
@end
